//
//  VisitorViewController.swift
//  SinaWeibo
//
//  Created by 许良 on 16/6/1.
//  Copyright © 2016年 fading. All rights reserved.
//

import UIKit

//定义代理
protocol VisitorViewDelegate : NSObjectProtocol {
    
    func loginBtnWillClick()
    
    func registerBtnWillClick()
}



class VisitorView: UIView {

    
    weak var deleagte : VisitorViewDelegate?
    
    func setupVisitorInfo (isHome : Bool, imageName: String, message: String) {
        
        iconView.hidden = !isHome
        homeicon.image = UIImage(named: imageName)
        messageLabel.text = message
        
        if isHome {
            startAnimation()
            
        }
    }
    
   
    
    func loginBtnClick () {
        deleagte?.loginBtnWillClick()
        
    }
    
    func registerBtnClick () {
        deleagte?.registerBtnWillClick()
    }
    
    
     override init (frame : CGRect) {
        super.init(frame : frame)
        setupLazyView()
    }
    
    func setupLazyView () {
        
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeicon)
        
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //子控件布局
        iconView.Fading_AlignInner(type: Fading_AlignType.Center, referView: self, size: nil)
        homeicon.Fading_AlignInner(type: Fading_AlignType.Center, referView: self, size: nil)
        messageLabel.Fading_AlignVertical(type: Fading_AlignType.BottomCenter, referView: iconView, size: nil)
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 244)
        addConstraint(widthCons)
        registerBtn.Fading_AlignVertical(type: Fading_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100 ,height : 30), offset: CGPoint(x:0,y:20))
        loginBtn.Fading_AlignVertical(type: Fading_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100 ,height : 30), offset: CGPoint(x:0,y:20))
        maskBGView.Fading_Fill(self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    private lazy var iconView : UIImageView = {
        let iconV = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iconV
    }()
    
    private lazy var maskBGView : UIImageView = {
        let maView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return maView
    }()
    //标志图标
    private lazy var homeicon : UIImageView = {
        let homeIC = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return homeIC
    }()
    
    
    //文本
    private lazy var messageLabel : UILabel = {
        let messageLa = UILabel()
        messageLa.numberOfLines = 0
        messageLa.textColor = UIColor.darkGrayColor()
        messageLa.text = "登陆后，别人评论你的消息，你都会在这里收到通知"
        return messageLa
    }()
    
    //登录按钮
    private lazy var loginBtn : UIButton = {
        
        let loginBtn = UIButton()
        
        loginBtn.setTitle("登录", forState: .Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        loginBtn.addTarget(self, action: #selector(VisitorView.loginBtnClick), forControlEvents: .TouchUpInside)
        return loginBtn
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
        
        let registerBtn = UIButton()
        
        registerBtn.setTitle("注册", forState: .Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        registerBtn.addTarget(self, action: #selector(VisitorView.registerBtnClick), forControlEvents: .TouchUpInside)
        
        return registerBtn
    }()
    
    // MARK: - 背景动画
    private func startAnimation () {
        
        //创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
}
